import { inject, injectable } from "inversify";
import { PrismaDB } from "../db";
import { UserDto } from "./user.dto";
import { plainToClass } from "class-transformer"; //把值合并到类中
import { validate } from "class-validator"; //验证
import { JWT } from "../jwt";
//提供一个注射器，用于创建UserService的实例
@injectable()
export class UserService {
  constructor(
    @inject(PrismaDB) private readonly PrismaDB: PrismaDB,
    @inject(JWT) private readonly jwt: JWT
  ) {}
  public async getList() {
    return this.PrismaDB.prisma.user.findMany();
  }
  public async createUser(user: UserDto) {
    let userDto = plainToClass(UserDto, user);
    const errors = await validate(userDto);
    console.log("errors: ", errors);
    if (errors.length) {
      return errors;
    } else {
      const result = await this.PrismaDB.prisma.user.create({
        data: user,
      });
      return {
        ...result,
        token: this.jwt.createToken(result),
      };
    }
  }
}
